package com.chenfan.mcn.enums;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:   tangwei
 * Date:     2021/2/23 16:49
 * Description: 收入合同变更内容类型
 */
@AllArgsConstructor
@SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
public enum AgreementChangeContentEnum {

    /**
     * 收入合同变更内容类型
     */

    CONTRACT_AMOUNT(1, "合同金额", false),

    EXCUTION_ORDER(2, "执行单", false),

    OTHER(3, "其他", false),

    INCOME_CONTRACT(4, "收入合同", false),

    INVOICE_INFO(5, "开票信息", false),

    COMPANY_CONTRACT_SUBJECT(6, "我司合同主体", true),

    PLATFORM_ORDER_NUMBER(7, "形式合同平台订单号", true),
    ;


    @Getter
    private Integer type;

    @Getter
    private String desc;

    @Getter
    private boolean useStatus;

    public static AgreementChangeContentEnum getByType(Integer type) {
        for (AgreementChangeContentEnum value : AgreementChangeContentEnum.values()) {
            if (value.type.equals(type)) {
                return value;
            }
        }
        return null;
    }

    /**
     * 是否含有
     *
     * @param changeContent
     * @param enums
     * @return
     */
    public static boolean contains(String changeContent, AgreementChangeContentEnum... enums) {
        if(StringUtils.isBlank(changeContent) || ArrayUtils.isEmpty(enums)) {
            return false;
        }
        List<Integer> typeList = JSONObject.parseArray(changeContent, Integer.class);
        if (CollectionUtils.isEmpty(typeList)) {
            return false;
        }
        List<Integer> candidates = new ArrayList<>(enums.length);
        for(AgreementChangeContentEnum item : enums) {
            candidates.add(item.getType());
        }
        return CollectionUtils.containsAny(typeList, candidates);
    }

    /**
     *
     * @param changeContent
     * @return
     */
    public static boolean containsInvalid(String changeContent) {
        if(StringUtils.isBlank(changeContent)) {
            return false;
        }
        List<Integer> typeList = JSONObject.parseArray(changeContent, Integer.class);
        if (CollectionUtils.isEmpty(typeList)) {
            return false;
        }
        for(AgreementChangeContentEnum item : AgreementChangeContentEnum.values()) {
            if(typeList.contains(item.getType()) && !item.useStatus) {
                return true;
            }
        }
        return false;
    }

    /**
     * 存在变更金额
     *
     * @param changeContent
     * @return
     */
    public static boolean containsAmount(String changeContent) {
        return contains(changeContent, AgreementChangeContentEnum.CONTRACT_AMOUNT);
    }

    /**
     * 存在变更执行单
     *
     * @param changeContent
     * @return
     */
    public static boolean containsExcOrder(String changeContent) {
        return contains(changeContent, AgreementChangeContentEnum.EXCUTION_ORDER);
    }

    /**
     * 存在收入合同变更
     * @param changeContent
     * @return
     */
    public static boolean containsIncomeContact(String changeContent) {
        return contains(changeContent, AgreementChangeContentEnum.INCOME_CONTRACT);
    }

    /**
     * 存在开票信息
     * @param changeContent
     * @return
     */
    /*public static boolean containsInvoiceInfo(String changeContent) {
        return contains(changeContent, AgreementChangeContentEnum.INVOICE_INFO);
    }*/

    /**
     * 存在我司合同主体变更
     * @param changeContent
     * @return
     */
    public static boolean containsCompanyContractSubject(String changeContent) {
        return contains(changeContent, AgreementChangeContentEnum.COMPANY_CONTRACT_SUBJECT);
    }

    /**
     * 存在变更形式合同平台订单号
     *
     * @param changeContent
     * @return
     */
    public static boolean containsPlatformOrderCode(String changeContent) {
        return contains(changeContent, AgreementChangeContentEnum.PLATFORM_ORDER_NUMBER);
    }
}